package com.birdboot.controller;

import com.birdboot.entity.User;
import com.birdboot.http.HttpServletRequest;
import com.birdboot.http.HttpServletResponse;

import java.io.*;
import java.net.URISyntaxException;

/*
* 处理与用户相关的业务
* */
public class UserController {
    private static File userDir;//表示存放所有用户信息的目录：users
    static {
        userDir = new File("./users");
        if(!userDir.exists()){
            userDir.mkdir();
        }
    }
    public void reg(HttpServletRequest request, HttpServletResponse response){
        System.out.println("开始处理用户注册");
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        String nickname = request.getParameter("nickname");
        String ageStr = request.getParameter("age");
        //必要的验证
        if( username==null || username.isEmpty() ||
            password==null || password.isEmpty() ||
            nickname==null || nickname.isEmpty() ||
            ageStr==null || ageStr.isEmpty() ||
            !ageStr.matches("[0-9]+")
        ){
            response.sendRedirect("/reg_info_error.html");
            return;
        }
        System.out.println(username+','+password+','+nickname+','+ageStr);
        int age = Integer.parseInt(ageStr); //将年龄转换为int值
        //2.
        User user = new User(username,password,nickname,age);
        /*
         * File的构造器
         * File(File parent,String child)
         * 创建一个File对象表示child这个子项，而它是在parent这个File对象所表示的目录中*/
        File file = new File(userDir,username+".obj");
        if(file.exists()){
            response.sendRedirect("/have_user.html");
            return;
        }
        try(
            FileOutputStream fos = new FileOutputStream(file);
            ObjectOutputStream oos = new ObjectOutputStream(fos);
        ) {
            oos.writeObject(user); //保存用户信息完毕
            //3.给用户回馈注册成功页面
            //要求浏览器重新访问下述地址对应的页面
            response.sendRedirect("/reg_success.html");
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
    public void login(HttpServletRequest request,HttpServletResponse response){
        System.out.println("开始处理用户登录！！！");
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        System.out.println(username+password);
        if(username==null||username.isEmpty()||password==null||password.isEmpty()){
            response.sendRedirect("/login_info_error.html");
            return;
        }
        File file = new File(userDir,username+".obj");
        if(file.exists()){
            //读取该文件并反序列化其中保存的user对象(该对象记录了曾经该用户的注册信息)
            try(
                    FileInputStream fis = new FileInputStream(file);
                    ObjectInputStream ois = new ObjectInputStream(fis);
            ) {
                User user = (User)ois.readObject();
                if(user.getPassword().equals(password)){
                    response.sendRedirect("/login_success.html");
                    return;
                }
            } catch (IOException | ClassNotFoundException e) {
                e.printStackTrace();
            }
        }
        //文件不存在说明该用户不存在--登录失败
        response.sendRedirect("/login_fail.html");
    }
}
